﻿using System;

namespace Asteroids.Game.PowerUps
{
    public abstract class TimedPowerUp : PowerUp
    {
        public Int32 PowerUpDuration { get; set; }

        private int _ticks;
        private Player _player;

        protected TimedPowerUp(Coordinate location) : base(location)
        {
            _ticks = 0;
        }

        public void Subscribe(Player player)
        {
            MainWindow.Timer.Tick += OnGameTick;
            _player = player;
        }

        public void OnGameTick(object sender, EventArgs args)
        {
            if (!Game.Instance.IsPaused)
            {
                Console.WriteLine("TimedPowerUp tick");
                if (_ticks++ >= PowerUpDuration)
                {
                    MainWindow.Timer.Tick -= OnGameTick;
                    UndoPerformAction(_player);
                }
            }
        }

        protected abstract void UndoPerformAction(Player player);
    }
}